package com.wk.dao.impl;

import java.io.InputStream;
import java.util.Map;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsOperations; 
import org.springframework.stereotype.Repository;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; 
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSFile;
import com.wk.dao.IGridFsDAO;

/**
 * Mongodb文件操作. <br>
 * @author chenxiangbai <br>
 * @version 1.0.0 2014-1-7 下午4:29:07 <br>
 * @see IGridFsDAO
 * @since JDK 1.6.0
 */
@Repository(value="fsDao")
public class GridFsDAO implements IGridFsDAO {

 	@Autowired
	private GridFsOperations gridFsTemplate; 
  
	
	@Override
	public String storeFile(InputStream content, String filename) {
		final GridFSFile file = this.gridFsTemplate.store(content, filename);
		return file.getId().toString();
	}
	
	@Override
	public String storeFile(InputStream content, String filename,String contentType) {
		final GridFSFile file = this.gridFsTemplate.store(content, filename,contentType);
		return file.getId().toString();
	}

	@Override
	public void deleteFile(String fileId) {
		Query query = buildFileIdQuery(fileId);
		this.gridFsTemplate.delete(query);
	}

	/**
	 * 构造fileId 的查询条件
	 * @param fileId
	 * @return
	 */
	private Query buildFileIdQuery(final String fileId) {
		final Criteria criteria = Criteria.where("_id").is(new ObjectId(fileId));
		final Query query = new Query(criteria);
		return query;
	}

	@Override
	public InputStream getInputStream(String fileId) {
		Query query = buildFileIdQuery(fileId);
		final GridFSDBFile file = gridFsTemplate.findOne(query);
		if (file != null) {
			return file.getInputStream();
		}
		return null;
	}
	
	@Override
	public GridFSDBFile getFile(String fileId) {
		if(null == fileId){
			return null;
		}
		Query query = buildFileIdQuery(fileId);
		final GridFSDBFile file = gridFsTemplate.findOne(query);
		return file;
	}

	@Override
	public String storeFile(InputStream content, String filename,
			String contentType, Map<String,Object> object) {
		GridFSFile file = null;
		if(object!=null && object.size()>0){
			DBObject obj = new BasicDBObject();
			obj.putAll(object);
			file = this.gridFsTemplate.store(content, filename, contentType, obj);
		}else{
			file = this.gridFsTemplate.store(content, filename, contentType);
		}
		return file.getId().toString();
	}

	@Override
	public GridFSFile storeGridFile(InputStream content, String filename,
			String contentType) {
		 
		return this.gridFsTemplate.store(content, filename,contentType);
	}
	
//	@Override
//	public GridFSDBFile getFileByFileName(String fileName) {	
//	gridOperation.findOne(new Query(Criteria.where("filename").is(filename))); 
//		final GridFSDBFile file = this.operations.findOne(Query.query(QueryParamCreator.eq("_id", "{ '$oid' : '53197dd4ffcb7141f869cd34'}")));
//		return file;
//	}
	 
}
